<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>医生后台 - 哈尔滨信息工程学院医院门诊系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }

        .header {
            background-color: #4CAF50;
            color: white;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            margin: 0;
            font-size: 24px;
        }

        .header .user-info {
            display: flex;
            align-items: center;
        }

        .header .user-info span {
            margin-right: 15px;
        }

        .header .logout-btn {
            background-color: #f44336;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
        }

        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }

        .stats-container {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }

        .stat-card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
            flex: 1;
            text-align: center;
        }

        .stat-card h3 {
            margin: 0 0 10px 0;
            color: #333;
        }

        .stat-card .number {
            font-size: 36px;
            font-weight: bold;
            color: #4CAF50;
        }

        .patients-container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
        }

        .patients-container h2 {
            margin-top: 0;
            color: #333;
        }

        .filter-container {
            display: flex;
            gap: 10px;
            margin-bottom: 15px;
        }

        .filter-container select {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #f2f2f2;
        }

        tr:hover {
            background-color: #f5f5f5;
        }

        .status-badge {
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
        }

        .status-pending {
            background-color: #ffeb3b;
            color: #333;
        }

        .status-processing {
            background-color: #2196F3;
            color: white;
        }

        .status-completed {
            background-color: #4CAF50;
            color: white;
        }

        .action-btn {
            background-color: #2196F3;
            color: white;
            border: none;
            padding: 6px 12px;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 5px;
        }

        .action-btn:hover {
            background-color: #0b7dda;
        }

        .action-btn.complete {
            background-color: #4CAF50;
        }

        .action-btn.complete:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>哈尔滨信息工程学院医院门诊系统 - 医生后台</h1>
        <div class="user-info">
            <span id="doctorName">医生</span>
            <a href="/hospital/index" class="logout-btn">退出登录</a>
        </div>
    </div>

    <div class="container">
        <div class="stats-container">
            <div class="stat-card">
                <h3>待就诊患者</h3>
                <div class="number" id="pendingCount">0</div>
            </div>
            <div class="stat-card">
                <h3>就诊中患者</h3>
                <div class="number" id="processingCount">0</div>
            </div>
            <div class="stat-card">
                <h3>今日已完成</h3>
                <div class="number" id="completedCount">0</div>
            </div>
        </div>

        <div class="patients-container">
            <h2>患者列表</h2>
            <div class="filter-container">
                <select id="statusFilter" onchange="filterPatients()">
                    <option value="">全部状态</option>
                    <option value="0">待就诊</option>
                    <option value="1">就诊中</option>
                    <option value="2">已完成</option>
                </select>
                <input type="date" id="dateFilter" onchange="filterPatients()">
            </div>

            <table>
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>年龄</th>
                        <th>联系电话</th>
                        <th>就诊科室</th>
                        <th>就诊时间</th>
                        <th>症状描述</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="patientTableBody">
                    <!-- 数据将通过AJAX加载 -->
                </tbody>
            </table>
        </div>
    </div>

    <script src="/js/jquery.min.js"></script>
    <script>
        $(function() {
            loadPatients();
            updateStats();
        });

        function loadPatients() {
            $.ajax({
                url: "/hospital/doctor/myPatients",
                type: "GET",
                success: function(data) {
                    if (data) {
                        renderPatientTable(data);
                        updateStats();
                    }
                },
                error: function() {
                    alert("加载患者数据失败");
                }
            });
        }

        function renderPatientTable(patients) {
            var tbody = $("#patientTableBody");
            tbody.empty();

            if (patients.length === 0) {
                tbody.append("<tr><td colspan='10' style='text-align:center;'>暂无患者数据</td></tr>");
                return;
            }

            patients.forEach(function(patient, index) {
                var statusText = "";
                var statusClass = "";

                switch(patient.status) {
                    case 0:
                        statusText = "待就诊";
                        statusClass = "status-pending";
                        break;
                    case 1:
                        statusText = "就诊中";
                        statusClass = "status-processing";
                        break;
                    case 2:
                        statusText = "已完成";
                        statusClass = "status-completed";
                        break;
                }

                var row = "<tr>" +
                    "<td>" + (index + 1) + "</td>" +
                    "<td>" + patient.name + "</td>" +
                    "<td>" + patient.gender + "</td>" +
                    "<td>" + patient.age + "</td>" +
                    "<td>" + patient.phone + "</td>" +
                    "<td>" + patient.department + "</td>" +
                    "<td>" + patient.appointmentDate + " " + patient.appointmentTime + "</td>" +
                    "<td>" + patient.symptoms + "</td>" +
                    "<td><span class='status-badge " + statusClass + "'>" + statusText + "</span></td>" +
                    "<td>" +
                    (patient.status == 0 ? "<button class='action-btn' onclick='updateStatus(" + patient.id + ", 1)'>开始就诊</button>" : "") +
                    (patient.status == 1 ? "<button class='action-btn complete' onclick='updateStatus(" + patient.id + ", 2)'>完成就诊</button>" : "") +
                    "</td>" +
                    "</tr>";

                tbody.append(row);
            });
        }

        function updateStatus(patientId, newStatus) {
            $.ajax({
                url: "/hospital/doctor/updatePatientStatus",
                type: "POST",
                data: {
                    patientId: patientId,
                    status: newStatus
                },
                success: function(data) {
                    if (data > 0) {
                        loadPatients();
                    } else {
                        alert("更新状态失败");
                    }
                },
                error: function() {
                    alert("系统错误，请稍后再试");
                }
            });
        }

        function filterPatients() {
            // 这里可以实现更复杂的过滤逻辑
            loadPatients();
        }

        function updateStats() {
            var pendingCount = 0;
            var processingCount = 0;
            var completedCount = 0;

            $("#patientTableBody tr").each(function() {
                var statusBadge = $(this).find(".status-badge");
                if (statusBadge.hasClass("status-pending")) {
                    pendingCount++;
                } else if (statusBadge.hasClass("status-processing")) {
                    processingCount++;
                } else if (statusBadge.hasClass("status-completed")) {
                    completedCount++;
                }
            });

            $("#pendingCount").text(pendingCount);
            $("#processingCount").text(processingCount);
            $("#completedCount").text(completedCount);
        }
    </script>
</body>
</html>
